package com.xpc.config;

import com.xpc.shiro.realm.AccountUserRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Qinkuer
 * @version 1.0.0
 * @ProjectName UniversityGradeAdministrationSystem
 * @ClassName ShiroConfig.java
 * @Description 关于Shiro的配置类
 * @createTime 2022年05月21日 10:43:00
 */
@Configuration
public class ShiroConfig {




    /**
        * @description shiro过滤器,限制请求
        * @deprecated
        * @param defaultWebSecurityManager shiro的管理器
        * @return  org.apache.shiro.spring.web.ShiroFilterFactoryBean
        * @author Qinkuer
        * @Date  2022/5/21
        */
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //给Filter设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        //配置系统的受限资源
        Map<String ,String> map = new HashMap<>();
//        map.put("/user/login","anon");   //anon：设置为公共资源
//        map.put("/user/register","anon");
//        map.put("/**","authc"); //authc：表示请求这个资源需要认证和授权

        map.put("/**","anon");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;
    }


    /**
        * @description shiro安全管理器
        * @deprecated
        * @param realm  账号信息域.获取账号密码,盐,角色,权限
        * @return  org.apache.shiro.web.mgt.DefaultWebSecurityManager
        * @author Qinkuer
        * @Date  2022/5/21
        */
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(Realm realm){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();

        //给安全管理器设置Realm
        defaultWebSecurityManager.setRealm(realm);

        return defaultWebSecurityManager;
    }


    /**
        * @description 获取配置好的账号信息域.该域对密码进行md5加密,不进行散列加密.该域开启缓存.后续有需要可以把缓存改成Redis
        * @deprecated
        * @return  org.apache.shiro.realm.Realm
        * @author Qinkuer
        * @Date  2022/5/21
        */
    @Bean
    public Realm getRealm(){
        AccountUserRealm accountUserRealm = new AccountUserRealm();

        // 修改凭证校验匹配器
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        // 设置加密算法为md5
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        accountUserRealm.setCredentialsMatcher(hashedCredentialsMatcher);

        // 开启缓存管理
        accountUserRealm.setCacheManager(new EhCacheManager());
        //开启全局缓存
        accountUserRealm.setCachingEnabled(true);
        //开启认证缓存
        accountUserRealm.setAuthenticationCachingEnabled(true);
        accountUserRealm.setAuthenticationCacheName("authenticationCache");
        //开启授权缓存
        accountUserRealm.setAuthorizationCachingEnabled(true);
        accountUserRealm.setAuthorizationCacheName("authorizationCache");


        return accountUserRealm;
    }


//    //与shiro的生命周期相关的 能使@RequiresRoles注解生效
//    @Bean
//    public static LifecycleBeanPostProcessor getLifecycleBeanPostProcessor() {
//        return new LifecycleBeanPostProcessor();
//    }
//    @Bean
//    public static DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator(){
//        return new DefaultAdvisorAutoProxyCreator();
//    }


}



